<template>
  <div class="login-view">
    <div class="login-view__box">
      <el-form :model="loginForm" :rules="rules" ref="loginForm">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" clearable show-password>
            <template v-slot:prefix>
              <i class="el-input__icon el-icon-lock"></i>
            </template>
          </el-input>
        </el-form-item>
        <el-checkbox v-model="loginForm.remember">记住密码</el-checkbox>
        <el-button type="danger" @click="onLogin">登 录</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import * as storage from "@/utils/storage";
import { mapActions } from "vuex";
export default {
  name: "LoginView",
  data() {
    const validateUsername = (rule, value, callback) => {
      //用户名必须要以字母开头
      if (/^[a-zA-Z]/.test(value)) {
        return callback();
      } else {
        return callback(new Error("用户名必须以字母开头！"));
      }
    };

    return {
      loginForm: {
        username: "",
        password: "",
        remember: false,
      },
      rules: {
        username: [
          { validator: validateUsername, trigger: "submit" },
          // required 必须的           trigger  触发事件
          { required: true, message: "请输入用户名！", trigger: "blur" },
          { min: 4, max: 16, message: "用户名在5-16位之间", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码！", trigger: "blur" },
          { min: 5, max: 16, message: "密码在5-16位之间", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    ...mapActions(["login"]),
    async onLogin() {
      try {
        await this.$refs.loginForm.validate();
        // 发出登陆异步接口请求，返回promise对象
        // const res = await this.$api.auth(this.loginForm);
        // console.log(res);
        // storage.set("z-admin-token", res.token);
        // console.log(this.loginForm);
        await this.login(this.loginForm);

        if (this.loginForm.remember) {
          storage.set("z-admin-login", this.loginForm);
        }

        this.$message.success("登陆成功！");

        // 登陆成功后跳转到首页
        await this.$router.replace({ path: "/" });
        // console.log(res);
      } catch (e) {
        console.log(e);
      }
    },
  },
  created() {
    // 从本地存储中取出登录数据
    const loginData = storage.get("z-admin-login");
    if (loginData && loginData.remember) {
      this.loginForm = loginData;
    }
  },
  watch: {
    ["loginForm.remember"](val) {
      if (!val) {
        storage.remove("z-admin-login");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.login-view {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
  background: url(/img/bg2.png) no-repeat;
  background-size: 100% 100%;

  &__box {
    width: 442px;
    height: 422px;
    background-color: rgba(255, 255, 255, 0.4);
    border-radius: 10px;
  }
}
</style>
